import com.google.gson.Gson;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class DeleteBook extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        response.setContentType("application/json;charset=UTF-8");

        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();

        try {
            String number = request.getParameter("number");

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/linux_exam?useUnicode=true&characterEncoding=UTF-8",
                "root", "1qaz@WSX#EDC");

            PreparedStatement ps = conn.prepareStatement("DELETE FROM t_book WHERE number = ?");
            ps.setString(1, number);
            int rows = ps.executeUpdate();

            result.put("success", rows > 0);
            result.put("message", rows > 0 ? "删除成功" : "删除失败");

            ps.close();
            conn.close();
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }

        out.print(new Gson().toJson(result));
    }
}

